package com.rightsidetech.http.controller.agentApp.ride;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.rightsidetech.activity.dto.request.ride.AgainSubmitRideReq;
import com.rightsidetech.activity.dto.request.ride.AgentSaveRideReq;
import com.rightsidetech.activity.dto.request.ride.AgentUpPictureReq;
import com.rightsidetech.activity.dto.request.ride.ApplyRideReq;
import com.rightsidetech.activity.dto.response.ActRideDetailResp;
import com.rightsidetech.activity.dto.response.AgentRideResp;
import com.rightsidetech.activity.service.ActRideService;
import com.rightsidetech.commom.base.PageRequest;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.system.entity.SysAgentInfo;
import com.rightsidetech.system.entity.SysAgentLoginInfo;
import com.rightsidetech.system.entity.SysCompanyArea;
import com.rightsidetech.system.entity.store.SysStoreInfo;
import com.rightsidetech.system.service.SysAgentInfoService;
import com.rightsidetech.system.service.SysCompanyAreaService;
import com.rightsidetech.system.service.SysStoreInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.Calendar;
import java.util.Date;

@RestController
@RequestMapping("/agentApp/ride")
@ShenyuSpringMvcClient(path = "/agentApp/ride/**")
@Api(tags = "代理骑行活动相关")
public class AgentRideController extends BaseController {

    @Resource
    private SysAgentInfoService sysAgentInfoService;
    @Resource
    private SysStoreInfoService sysStoreInfoService;
    @Resource
    private ActRideService actRideService;
    @Resource
    private SysCompanyAreaService sysCompanyAreaService;

    @Log
    @Token
    @PostMapping("/apply")
    @ApiOperation("申请骑行活动")
    public ResponseData applyAct(@RequestBody AgentSaveRideReq req){
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        ResponseData data = checkReq(req);
        if(data != null){
            return data;
        }
        Long loginInfoId = Long.parseLong(getAttribute());
        SysAgentLoginInfo loginInfo = sysAgentInfoService.selectInfoById(loginInfoId);
        if(ObjectUtil.isEmpty(loginInfo)){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"未找到登录人");
        }
        SysAgentInfo sysAgentInfo = sysAgentInfoService.selectSysAgentInfo(loginInfo.getAgentInfoId());
        if(ObjectUtil.isEmpty(sysAgentInfo)){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"未找到登录人");
        }
        SysCompanyArea sysCompanyArea = sysCompanyAreaService.selectById(sysAgentInfo.getAreaId());
        if (ObjectUtil.isEmpty(sysCompanyArea)){
            if(ObjectUtil.isEmpty(sysAgentInfo)){
                return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"未找到大区信息");
            }
        }
        SysStoreInfo storeInfo = sysStoreInfoService.selectByPrimaryKey(req.getStoreId());
        if(ObjectUtil.isEmpty(storeInfo)){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"未找到该门店");
        }
        if(storeInfo.getStatus() != 1){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"此门店已经关闭");
        }
        if(!storeInfo.getSysAgentInfoId().equals(loginInfo.getAgentInfoId())){
            return ResponseData.fail("门店不是该代理商下的门店");
        }
        int count = sysAgentInfoService.countStatus(loginInfo.getAgentInfoId());
        if(count>=3){
            return ResponseData.fail("当月申请成功次数已达三次");
        }
        ApplyRideReq actReq = new ApplyRideReq();
        actReq.setAreaId(sysAgentInfo.getAreaId());
        actReq.setAreaName(sysCompanyArea.getName());
        actReq.setAgentInfoId(loginInfo.getAgentInfoId());
        actReq.setAgentName(sysAgentInfo.getName());
        actReq.setAgentLoginId(loginInfo.getId());
        actReq.setAgentLoginName(loginInfo.getLoginName());
        actReq.setStoreName(storeInfo.getStoreName());
        actReq.setStoreId(storeInfo.getId());
        actReq.setModel(req.getModel());
        actReq.setStartTime(req.getStartTime());
        actReq.setEndTime(req.getEndTime());
        return actRideService.applyRide(actReq);
    }

    private ResponseData<Object> checkReq(AgentSaveRideReq req){
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)-1,23,59,59);
        Date time = calendar.getTime();
        if(req.getStartTime().compareTo(time) < 0){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"申请活动开始时间不可小于今天");
        }
        if(req.getEndTime().compareTo(req.getStartTime()) < 0){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"申请活动结束不可小于开始时间");
        }
        return null;
    }

    @Log
    @Token
    @GetMapping("/page")
    @ApiOperation("分页查询骑行活动")
    public ResponseData<PageInfo<AgentRideResp>> page(PageRequest req){
        Long loginInfoId = Long.parseLong(getAttribute());
        if(ObjectUtil.isEmpty(loginInfoId)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }
        //获取代理商id
        SysAgentLoginInfo loginInfo = sysAgentInfoService.selectInfoById(loginInfoId);
        if (ObjectUtils.isEmpty(loginInfo)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }

        return actRideService.agentPage(req, loginInfo.getAgentInfoId());
    }

    @Log
    @Token
    @PostMapping("/up_picture")
    @ApiOperation("上传骑行活动图片")
    public ResponseData upPic(@RequestBody AgentUpPictureReq req){
        Long loginInfoId = Long.parseLong(getAttribute());
        if(ObjectUtil.isEmpty(loginInfoId)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }
        SysAgentLoginInfo loginInfo = sysAgentInfoService.selectInfoById(loginInfoId);
        if(ObjectUtil.isEmpty(loginInfo)){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"未找到登录人");
        }
        return actRideService.upPicture(req,loginInfoId,loginInfo.getLoginName());
    }

    @Log
    @Token
    @GetMapping("/detail")
    @ApiOperation("骑行活动详情")
    public ResponseData<ActRideDetailResp> detail(@RequestParam(value = "id",required = true) Long id){
        Long loginInfoId = Long.parseLong(getAttribute());
        if(ObjectUtil.isEmpty(loginInfoId)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }
        return actRideService.selectDetail(id,loginInfoId);
    }

    @Log
    @Token
    @PostMapping("/againSub")
    @ApiOperation("重新提交骑行活动")
    public ResponseData againSub(@RequestBody AgainSubmitRideReq req){
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        if(req.getEndTime().compareTo(req.getStartTime()) < 0){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"申请活动结束不可小于开始时间");
        }
        Long loginInfoId = Long.parseLong(getAttribute());
        SysAgentLoginInfo loginInfo = sysAgentInfoService.selectInfoById(loginInfoId);
        if(ObjectUtil.isEmpty(loginInfo)){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"未找到登录人");
        }
        SysAgentInfo sysAgentInfo = sysAgentInfoService.selectSysAgentInfo(loginInfo.getAgentInfoId());
        if(ObjectUtil.isEmpty(sysAgentInfo)){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"未找到登录人");
        }
        SysStoreInfo storeInfo = sysStoreInfoService.selectByPrimaryKey(req.getStoreId());
        if(ObjectUtil.isEmpty(storeInfo)){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"未找到该门店");
        }
        if(!storeInfo.getSysAgentInfoId().equals(loginInfo.getAgentInfoId())){
            return ResponseData.fail("门店不是该代理商下的门店");
        }
        ApplyRideReq actReq = new ApplyRideReq();
        actReq.setAgentInfoId(loginInfo.getAgentInfoId());
        actReq.setAgentName(sysAgentInfo.getName());
        actReq.setAgentLoginId(loginInfo.getId());
        actReq.setAgentLoginName(loginInfo.getLoginName());
        actReq.setStoreName(storeInfo.getStoreName());
        actReq.setStoreId(storeInfo.getId());
        actReq.setModel(req.getModel());
        actReq.setStartTime(req.getStartTime());
        actReq.setEndTime(req.getEndTime());
        return actRideService.againSubmit(req,actReq);
    }
}
